﻿using BeerXMLWPF.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace BeerXMLWPF.ViewModels
{
    public static class XMLParser
    {
        private static StringBuilder stringBuilder = new StringBuilder();
        public static Recipe ParseByXDocument(string xmlUrl)
        {
            var recipe = new Recipe();
            XDocument doc = XDocument.Load(xmlUrl);
            if (XMLValidate(xmlUrl))
            {
                XElement generalElement = doc
                    .Element("RECIPES")
                    .Element("RECIPE");
                recipe.Name = generalElement.Element("NAME").Value;
                recipe.Version = generalElement.Element("VERSION").Value;
                recipe.Type = generalElement.Element("TYPE").Value;
                recipe.Brewer = generalElement.Element("BREWER").Value;
                recipe.Asst_Brewer = generalElement.Element("ASST_BREWER").Value;
                recipe.Batch_Size = generalElement.Element("BATCH_SIZE").Value;
                recipe.Boil_Size = generalElement.Element("BOIL_SIZE").Value;
                recipe.Boil_Time = generalElement.Element("BOIL_TIME").Value;
                recipe.Efficiency = generalElement.Element("EFFICIENCY").Value;
                recipe.Hops = (from c in generalElement.Descendants("HOP")
                               select new Hop()
                               {
                                   Name = c.Element("NAME").Value,
                                   Version = c.Element("VERSION").Value,
                                   Origin = c.Element("ORIGIN").Value,
                                   Alpha = c.Element("ALPHA").Value,
                                   Amount = c.Element("AMOUNT").Value,
                                   Use = c.Element("USE").Value,
                                   Time = c.Element("TIME").Value,
                                   Notes = c.Element("NOTES").Value,
                                   Type = c.Element("TYPE").Value,
                                   Form = c.Element("FORM").Value,
                                   Beta = c.Element("BETA").Value,
                                   Hsi = c.Element("HSI").Value,
                                   Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                   Inventory = c.Element("INVENTORY").Value,
                                   Display_Time = c.Element("DISPLAY_TIME").Value,
                               }).ToList<Hop>();
                recipe.Fermentables = (from c in generalElement.Descendants("FERMENTABLE")
                                       select new Fermentable()
                                       {
                                           Name = c.Element("NAME").Value,
                                           Version = c.Element("VERSION").Value,
                                           Type = c.Element("TYPE").Value,
                                           Amount = c.Element("AMOUNT").Value,
                                           Yield = c.Element("YIELD").Value,
                                           Color = c.Element("COLOR").Value,
                                           Add_After_Boil = c.Element("ADD_AFTER_BOIL").Value,
                                           Origin = c.Element("ORIGIN").Value,
                                           Supplier = c.Element("SUPPLIER").Value,
                                           Notes = c.Element("NOTES").Value,
                                           Coarse_Fine_Diff = c.Element("COARSE_FINE_DIFF").Value,
                                           Moisture = c.Element("MOISTURE").Value,
                                           Diastatic_Power = c.Element("DIASTATIC_POWER").Value,
                                           Protein = c.Element("PROTEIN").Value,
                                           Max_In_Batch = c.Element("MAX_IN_BATCH").Value,
                                           Recommend_Mash = c.Element("RECOMMEND_MASH").Value,
                                           IBU_Gal_Per_LB = c.Element("IBU_GAL_PER_LB").Value,
                                           Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                           Inventory = c.Element("INVENTORY").Value,
                                           Potential = c.Element("POTENTIAL").Value,
                                           Display_Color = c.Element("DISPLAY_COLOR").Value,
                                           Extract_Substitude = c.Element("EXTRACT_SUBSTITUTE").Value,
                                       }).ToList<Fermentable>();
                recipe.Miscs = (from c in generalElement.Descendants("MISC")
                                select new Misc()
                                {
                                    Name = c.Element("NAME").Value,
                                    Version = c.Element("VERSION").Value,
                                    Type = c.Element("TYPE").Value,
                                    Amount = c.Element("AMOUNT").Value,
                                    Use = c.Element("USE").Value,
                                    Time = c.Element("TIME").Value,
                                    Amount_Is_Weight = c.Element("AMOUNT_IS_WEIGHT").Value,
                                    Use_For = c.Element("USE_FOR").Value,
                                    Notes = c.Element("NOTES").Value,
                                    Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                    Batch_Size = c.Element("BATCH_SIZE").Value,
                                    Inventory = c.Element("INVENTORY").Value,
                                    Display_Time = c.Element("DISPLAY_TIME").Value,
                                }).ToList<Misc>();
                recipe.Yeasts = (from c in generalElement.Descendants("YEAST")
                                 select new Yeast()
                                 {
                                     Name = c.Element("NAME").Value,
                                     Version = c.Element("VERSION").Value,
                                     Type = c.Element("TYPE").Value,
                                     Amount = c.Element("AMOUNT").Value,
                                     Form = c.Element("FORM").Value,
                                     Laboratory = c.Element("LABORATORY").Value,
                                     Amount_Is_Weight = c.Element("AMOUNT_IS_WEIGHT").Value,
                                     Product_ID = c.Element("PRODUCT_ID").Value,
                                     Notes = c.Element("NOTES").Value,
                                     Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                     Min_Temperature = c.Element("MIN_TEMPERATURE").Value,
                                     Max_Temperature = c.Element("MAX_TEMPERATURE").Value,
                                     Inventory = c.Element("INVENTORY").Value,
                                     Flocculation = c.Element("FLOCCULATION").Value,
                                     Attenuation = c.Element("ATTENUATION").Value,
                                     Best_For = c.Element("BEST_FOR").Value,
                                     Max_Reuse = c.Element("MAX_REUSE").Value,
                                     Time_Cultured = c.Element("TIMES_CULTURED").Value,
                                     Add_To_Secondary = c.Element("ADD_TO_SECONDARY").Value,
                                     Disp_Min_Temp = c.Element("DISP_MIN_TEMP").Value,
                                     Disp_Max_Temp = c.Element("DISP_MAX_TEMP").Value,
                                     Culture_Date = c.Element("CULTURE_DATE").Value,
                                 }).ToList<Yeast>();
                recipe.Waters = (from c in generalElement.Descendants("WATER")
                                 select new Water()
                                 {
                                     Name = c.Element("NAME").Value,
                                     Version = c.Element("VERSION").Value,
                                     Calcium = c.Element("CALCIUM").Value,
                                     Amount = c.Element("AMOUNT").Value,
                                     Bicarbonate = c.Element("BICARBONATE").Value,
                                     Sulfate = c.Element("SULFATE").Value,
                                     Chloride = c.Element("CHLORIDE").Value,
                                     Sodium = c.Element("SODIUM").Value,
                                     Notes = c.Element("NOTES").Value,
                                     Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                     Magiesium = c.Element("MAGNESIUM").Value,
                                     PH = c.Element("PH").Value,
                                 }).ToList<Water>();
                XElement style = generalElement.Element("STYLE");
                recipe.Style = new BeerXMLWPF.Models.Style()
                {
                    Name = style.Element("NAME").Value,
                    Version = style.Element("VERSION").Value,
                    Type = style.Element("TYPE").Value,
                    Category = style.Element("CATEGORY").Value,
                    Category_Number = style.Element("CATEGORY_NUMBER").Value,
                    Style_Letter = style.Element("STYLE_LETTER").Value,
                    Style_Guide = style.Element("STYLE_GUIDE").Value,
                    OG_Min = style.Element("OG_MIN").Value,
                    Notes = style.Element("NOTES").Value,
                    OG_Max = style.Element("OG_MAX").Value,
                    FG_Min = style.Element("FG_MIN").Value,
                    FG_Max = style.Element("FG_MAX").Value,
                    IBU_Min = style.Element("IBU_MIN").Value,
                    IBU_Max = style.Element("IBU_MAX").Value,
                    Color_Min = style.Element("COLOR_MIN").Value,
                    Color_Max = style.Element("COLOR_MAX").Value,
                    Carb_Min = style.Element("CARB_MIN").Value,
                    Carb_Max = style.Element("CARB_MAX").Value,
                    ABV_Min = style.Element("ABV_MIN").Value,
                    ABV_Max = style.Element("ABV_MAX").Value,
                    Profile = style.Element("PROFILE").Value,
                    Ingredients = style.Element("INGREDIENTS").Value,
                    Examples = style.Element("EXAMPLES").Value,
                    Display_Color_Max = style.Element("DISPLAY_COLOR_MAX").Value,
                    Display_Color_Min = style.Element("DISPLAY_COLOR_MIN").Value,
                    Display_OG_Max = style.Element("DISPLAY_OG_MAX").Value,
                    Display_OG_Min = style.Element("DISPLAY_OG_MIN").Value,
                    Display_FG_Max = style.Element("DISPLAY_FG_MAX").Value,
                    Display_FG_Min = style.Element("DISPLAY_FG_MIN").Value,
                    OG_Range = style.Element("OG_RANGE").Value,
                    FG_Range = style.Element("FG_RANGE").Value,
                    IBU_Range = style.Element("IBU_RANGE").Value,
                    ABV_Range = style.Element("ABV_RANGE").Value,
                    Carb_Range = style.Element("CARB_RANGE").Value,
                    Color_Range = style.Element("COLOR_RANGE").Value,
                };
                XElement equipment = generalElement.Element("EQUIPMENT");
                recipe.Equipment = new Equipment()
                {
                    Name = equipment.Element("NAME").Value,
                    Version = equipment.Element("VERSION").Value,
                    Boil_Size = equipment.Element("BOIL_SIZE").Value,
                    Batch_Size = equipment.Element("BATCH_SIZE").Value,
                    Tun_Volume = equipment.Element("TUN_VOLUME").Value,
                    Tun_Weight = equipment.Element("TUN_WEIGHT").Value,
                    Tun_Specific_Heat = equipment.Element("TUN_SPECIFIC_HEAT").Value,
                    Top_Up_Water = equipment.Element("TOP_UP_WATER").Value,
                    Trub_Chiller_Loss = equipment.Element("TRUB_CHILLER_LOSS").Value,
                    Evap_Rate = equipment.Element("EVAP_RATE").Value,
                    Boil_Time = equipment.Element("BOIL_TIME").Value,
                    Calc_Boil_Volume = equipment.Element("CALC_BOIL_VOLUME").Value,
                    Lauter_Deadspace = equipment.Element("LAUTER_DEADSPACE").Value,
                    Top_Up_Kettle = equipment.Element("TOP_UP_KETTLE").Value,
                    Hop_Utilization = equipment.Element("HOP_UTILIZATION").Value,
                    Cooling_Loss_Pct = equipment.Element("COOLING_LOSS_PCT").Value,
                    Notes = equipment.Element("NOTES").Value,
                    Display_Boil_Size = equipment.Element("DISPLAY_BOIL_SIZE").Value,
                    Display_Batch_Size = equipment.Element("DISPLAY_BATCH_SIZE").Value,
                    Display_Tun_Volume = equipment.Element("DISPLAY_TUN_VOLUME").Value,
                    Display_Tun_Weight = equipment.Element("DISPLAY_TUN_WEIGHT").Value,
                    Display_Top_Up_Water = equipment.Element("DISPLAY_TOP_UP_WATER").Value,
                    Display_Trub_Chiller_Loss = equipment.Element("DISPLAY_TRUB_CHILLER_LOSS").Value,
                    Display_Lauter_Deadspace = equipment.Element("DISPLAY_LAUTER_DEADSPACE").Value,
                    Display_Top_Up_Kettle = equipment.Element("DISPLAY_TOP_UP_KETTLE").Value,
                };
                XElement mash = generalElement.Element("MASH");
                recipe.Mash = new Mash()
                {
                    Name = mash.Element("NAME").Value,
                    Version = mash.Element("VERSION").Value,
                    Grain_Temp = mash.Element("GRAIN_TEMP").Value,
                    Tun_Temp = mash.Element("TUN_TEMP").Value,
                    Sparge_Temp = mash.Element("SPARGE_TEMP").Value,
                    PH = mash.Element("PH").Value,
                    Tun_Weight = mash.Element("TUN_WEIGHT").Value,
                    Tun_Specific_Heat = mash.Element("TUN_SPECIFIC_HEAT").Value,
                    Equip_Adjust = mash.Element("EQUIP_ADJUST").Value,
                    Notes = mash.Element("NOTES").Value,
                    Display_Grain_Temp = mash.Element("DISPLAY_GRAIN_TEMP").Value,
                    Display_Tun_Temp = mash.Element("DISPLAY_TUN_TEMP").Value,
                    Display_Sparge_Temp = mash.Element("DISPLAY_SPARGE_TEMP").Value,
                    Display_Tun_Weight = mash.Element("DISPLAY_TUN_WEIGHT").Value,
                    Mash_Steps = (from c in mash.Descendants("MASH_STEP")
                                  select new Mash_Step()
                                  {
                                      Name = c.Element("NAME").Value,
                                      Version = c.Element("VERSION").Value,
                                      Type = c.Element("TYPE").Value,
                                      Infuse_Amount = c.Element("INFUSE_AMOUNT").Value,
                                      Step_Time = c.Element("STEP_TIME").Value,
                                      Step_Temp = c.Element("STEP_TEMP").Value,
                                      Ramp_Time = c.Element("RAMP_TIME").Value,
                                      End_Temp = c.Element("END_TEMP").Value,
                                      Decsription = c.Element("DESCRIPTION").Value,
                                      Water_Grain_Ratio = c.Element("WATER_GRAIN_RATIO").Value,
                                      Decotion_Amt = c.Element("DECOCTION_AMT").Value,
                                      Infuse_Temp = c.Element("INFUSE_TEMP").Value,
                                      Display_Step_Temp = c.Element("DISPLAY_STEP_TEMP").Value,
                                      Display_Infuse_Amt = c.Element("DISPLAY_INFUSE_AMT").Value
                                  }).ToList<Mash_Step>()
                };
                recipe.Notes = generalElement.Element("NOTES").Value;
                recipe.Taste_Notes = generalElement.Element("TASTE_NOTES").Value;
                recipe.Taste_Rating = generalElement.Element("TASTE_RATING").Value;
                recipe.OG = generalElement.Element("OG").Value;
                recipe.FG = generalElement.Element("FG").Value;
                recipe.Carbonation = generalElement.Element("CARBONATION").Value;
                recipe.Fermentation_Stages = generalElement.Element("FERMENTATION_STAGES").Value;
                recipe.Primary_Age = generalElement.Element("PRIMARY_AGE").Value;
                recipe.Primary_Temp = generalElement.Element("PRIMARY_TEMP").Value;
                recipe.Secondary_Age = generalElement.Element("SECONDARY_AGE").Value;
                recipe.Secondary_Temp = generalElement.Element("SECONDARY_TEMP").Value;
                recipe.Tertiary_Age = generalElement.Element("TERTIARY_AGE").Value;
                recipe.Age = generalElement.Element("AGE").Value;
                recipe.Age_Temp = generalElement.Element("AGE_TEMP").Value;
                recipe.Carbonation_Used = generalElement.Element("CARBONATION_USED").Value;
                recipe.Forced_Carbonation = generalElement.Element("FORCED_CARBONATION").Value;
                recipe.Priming_Sugar_Name = generalElement.Element("PRIMING_SUGAR_NAME").Value;
                recipe.Priming_Sugar_Equiv = generalElement.Element("PRIMING_SUGAR_EQUIV").Value;
                recipe.Keg_Priming_Factor = generalElement.Element("KEG_PRIMING_FACTOR").Value;
                recipe.Carbonation_Temp = generalElement.Element("CARBONATION_TEMP").Value;
                recipe.Display_Carb_Temp = generalElement.Element("DISPLAY_CARB_TEMP").Value;
                recipe.Date = generalElement.Element("DATE").Value;
                recipe.EST_OG = generalElement.Element("EST_OG").Value;
                recipe.EST_FG = generalElement.Element("EST_FG").Value;
                recipe.EST_Color = generalElement.Element("EST_COLOR").Value;
                recipe.IBU = generalElement.Element("IBU").Value;
                recipe.IBU_Method = generalElement.Element("IBU_METHOD").Value;
                recipe.EST_ABV = generalElement.Element("EST_ABV").Value;
                recipe.ABV = generalElement.Element("ABV").Value;
                recipe.Actual_Efficiency = generalElement.Element("ACTUAL_EFFICIENCY").Value;
                recipe.Calories = generalElement.Element("CALORIES").Value;
                recipe.Display_Batch_Size = generalElement.Element("DISPLAY_BATCH_SIZE").Value;
                recipe.Display_Boil_Size = generalElement.Element("DISPLAY_BOIL_SIZE").Value;
                recipe.Display_OG = generalElement.Element("DISPLAY_OG").Value;
                recipe.Display_FG = generalElement.Element("DISPLAY_FG").Value;
                recipe.Display_Primary_Temp = generalElement.Element("DISPLAY_PRIMARY_TEMP").Value;
                recipe.Display_Secondary_Temp = generalElement.Element("DISPLAY_SECONDARY_TEMP").Value;
                recipe.Display_Tertiary_Temp = generalElement.Element("DISPLAY_TERTIARY_TEMP").Value;
                recipe.Display_Age_Temp = generalElement.Element("DISPLAY_AGE_TEMP").Value;
                return recipe;
            }
            else
            {
                MessageBox.Show("This xml file is not validate. \n" + stringBuilder.ToString());
                stringBuilder.Clear();
                return null;
            }
        }
        public static Recipe ParseByXDocument(XDocument doc)
        {
            var recipe = new Recipe();
            XElement generalElement = doc
                    .Element("RECIPES")
                    .Element("RECIPE");
            recipe.Name = generalElement.Element("NAME").Value;
            recipe.Version = generalElement.Element("VERSION").Value;
            recipe.Type = generalElement.Element("TYPE").Value;
            recipe.Brewer = generalElement.Element("BREWER").Value;
            recipe.Asst_Brewer = generalElement.Element("ASST_BREWER").Value;
            recipe.Batch_Size = generalElement.Element("BATCH_SIZE").Value;
            recipe.Boil_Size = generalElement.Element("BOIL_SIZE").Value;
            recipe.Boil_Time = generalElement.Element("BOIL_TIME").Value;
            recipe.Efficiency = generalElement.Element("EFFICIENCY").Value;
            recipe.Hops = (from c in generalElement.Descendants("HOP")
                           select new Hop()
                           {
                               Name = c.Element("NAME").Value,
                               Version = c.Element("VERSION").Value,
                               Origin = c.Element("ORIGIN").Value,
                               Alpha = c.Element("ALPHA").Value,
                               Amount = c.Element("AMOUNT").Value,
                               Use = c.Element("USE").Value,
                               Time = c.Element("TIME").Value,
                               Notes = c.Element("NOTES").Value,
                               Type = c.Element("TYPE").Value,
                               Form = c.Element("FORM").Value,
                               Beta = c.Element("BETA").Value,
                               Hsi = c.Element("HSI").Value,
                               Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                               Inventory = c.Element("INVENTORY").Value,
                               Display_Time = c.Element("DISPLAY_TIME").Value,
                           }).ToList<Hop>();
            recipe.Fermentables = (from c in generalElement.Descendants("FERMENTABLE")
                                   select new Fermentable()
                                   {
                                       Name = c.Element("NAME").Value,
                                       Version = c.Element("VERSION").Value,
                                       Type = c.Element("TYPE").Value,
                                       Amount = c.Element("AMOUNT").Value,
                                       Yield = c.Element("YIELD").Value,
                                       Color = c.Element("COLOR").Value,
                                       Add_After_Boil = c.Element("ADD_AFTER_BOIL").Value,
                                       Origin = c.Element("ORIGIN").Value,
                                       Supplier = c.Element("SUPPLIER").Value,
                                       Notes = c.Element("NOTES").Value,
                                       Coarse_Fine_Diff = c.Element("COARSE_FINE_DIFF").Value,
                                       Moisture = c.Element("MOISTURE").Value,
                                       Diastatic_Power = c.Element("DIASTATIC_POWER").Value,
                                       Protein = c.Element("PROTEIN").Value,
                                       Max_In_Batch = c.Element("MAX_IN_BATCH").Value,
                                       Recommend_Mash = c.Element("RECOMMEND_MASH").Value,
                                       IBU_Gal_Per_LB = c.Element("IBU_GAL_PER_LB").Value,
                                       Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                       Inventory = c.Element("INVENTORY").Value,
                                       Potential = c.Element("POTENTIAL").Value,
                                       Display_Color = c.Element("DISPLAY_COLOR").Value,
                                       Extract_Substitude = c.Element("EXTRACT_SUBSTITUTE").Value,
                                   }).ToList<Fermentable>();
            recipe.Miscs = (from c in generalElement.Descendants("MISC")
                            select new Misc()
                            {
                                Name = c.Element("NAME").Value,
                                Version = c.Element("VERSION").Value,
                                Type = c.Element("TYPE").Value,
                                Amount = c.Element("AMOUNT").Value,
                                Use = c.Element("USE").Value,
                                Time = c.Element("TIME").Value,
                                Amount_Is_Weight = c.Element("AMOUNT_IS_WEIGHT").Value,
                                Use_For = c.Element("USE_FOR").Value,
                                Notes = c.Element("NOTES").Value,
                                Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                Batch_Size = c.Element("BATCH_SIZE").Value,
                                Inventory = c.Element("INVENTORY").Value,
                                Display_Time = c.Element("DISPLAY_TIME").Value,
                            }).ToList<Misc>();
            recipe.Yeasts = (from c in generalElement.Descendants("YEAST")
                             select new Yeast()
                             {
                                 Name = c.Element("NAME").Value,
                                 Version = c.Element("VERSION").Value,
                                 Type = c.Element("TYPE").Value,
                                 Amount = c.Element("AMOUNT").Value,
                                 Form = c.Element("FORM").Value,
                                 Laboratory = c.Element("LABORATORY").Value,
                                 Amount_Is_Weight = c.Element("AMOUNT_IS_WEIGHT").Value,
                                 Product_ID = c.Element("PRODUCT_ID").Value,
                                 Notes = c.Element("NOTES").Value,
                                 Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                 Min_Temperature = c.Element("MIN_TEMPERATURE").Value,
                                 Max_Temperature = c.Element("MAX_TEMPERATURE").Value,
                                 Inventory = c.Element("INVENTORY").Value,
                                 Flocculation = c.Element("FLOCCULATION").Value,
                                 Attenuation = c.Element("ATTENUATION").Value,
                                 Best_For = c.Element("BEST_FOR").Value,
                                 Max_Reuse = c.Element("MAX_REUSE").Value,
                                 Time_Cultured = c.Element("TIMES_CULTURED").Value,
                                 Add_To_Secondary = c.Element("ADD_TO_SECONDARY").Value,
                                 Disp_Min_Temp = c.Element("DISP_MIN_TEMP").Value,
                                 Disp_Max_Temp = c.Element("DISP_MAX_TEMP").Value,
                                 Culture_Date = c.Element("CULTURE_DATE").Value,
                             }).ToList<Yeast>();
            recipe.Waters = (from c in generalElement.Descendants("WATER")
                             select new Water()
                             {
                                 Name = c.Element("NAME").Value,
                                 Version = c.Element("VERSION").Value,
                                 Calcium = c.Element("CALCIUM").Value,
                                 Amount = c.Element("AMOUNT").Value,
                                 Bicarbonate = c.Element("BICARBONATE").Value,
                                 Sulfate = c.Element("SULFATE").Value,
                                 Chloride = c.Element("CHLORIDE").Value,
                                 Sodium = c.Element("SODIUM").Value,
                                 Notes = c.Element("NOTES").Value,
                                 Display_Amount = c.Element("DISPLAY_AMOUNT").Value,
                                 Magiesium = c.Element("MAGNESIUM").Value,
                                 PH = c.Element("PH").Value,
                             }).ToList<Water>();
            XElement style = generalElement.Element("STYLE");
            recipe.Style = new BeerXMLWPF.Models.Style()
            {
                Name = style.Element("NAME").Value,
                Version = style.Element("VERSION").Value,
                Type = style.Element("TYPE").Value,
                Category = style.Element("CATEGORY").Value,
                Category_Number = style.Element("CATEGORY_NUMBER").Value,
                Style_Letter = style.Element("STYLE_LETTER").Value,
                Style_Guide = style.Element("STYLE_GUIDE").Value,
                OG_Min = style.Element("OG_MIN").Value,
                Notes = style.Element("NOTES").Value,
                OG_Max = style.Element("OG_MAX").Value,
                FG_Min = style.Element("FG_MIN").Value,
                FG_Max = style.Element("FG_MAX").Value,
                IBU_Min = style.Element("IBU_MIN").Value,
                IBU_Max = style.Element("IBU_MAX").Value,
                Color_Min = style.Element("COLOR_MIN").Value,
                Color_Max = style.Element("COLOR_MAX").Value,
                Carb_Min = style.Element("CARB_MIN").Value,
                Carb_Max = style.Element("CARB_MAX").Value,
                ABV_Min = style.Element("ABV_MIN").Value,
                ABV_Max = style.Element("ABV_MAX").Value,
                Profile = style.Element("PROFILE").Value,
                Ingredients = style.Element("INGREDIENTS").Value,
                Examples = style.Element("EXAMPLES").Value,
                Display_Color_Max = style.Element("DISPLAY_COLOR_MAX").Value,
                Display_Color_Min = style.Element("DISPLAY_COLOR_MIN").Value,
                Display_OG_Max = style.Element("DISPLAY_OG_MAX").Value,
                Display_OG_Min = style.Element("DISPLAY_OG_MIN").Value,
                Display_FG_Max = style.Element("DISPLAY_FG_MAX").Value,
                Display_FG_Min = style.Element("DISPLAY_FG_MIN").Value,
                OG_Range = style.Element("OG_RANGE").Value,
                FG_Range = style.Element("FG_RANGE").Value,
                IBU_Range = style.Element("IBU_RANGE").Value,
                ABV_Range = style.Element("ABV_RANGE").Value,
                Carb_Range = style.Element("CARB_RANGE").Value,
                Color_Range = style.Element("COLOR_RANGE").Value,
            };
            XElement equipment = generalElement.Element("EQUIPMENT");
            recipe.Equipment = new Equipment()
            {
                Name = equipment.Element("NAME").Value,
                Version = equipment.Element("VERSION").Value,
                Boil_Size = equipment.Element("BOIL_SIZE").Value,
                Batch_Size = equipment.Element("BATCH_SIZE").Value,
                Tun_Volume = equipment.Element("TUN_VOLUME").Value,
                Tun_Weight = equipment.Element("TUN_WEIGHT").Value,
                Tun_Specific_Heat = equipment.Element("TUN_SPECIFIC_HEAT").Value,
                Top_Up_Water = equipment.Element("TOP_UP_WATER").Value,
                Trub_Chiller_Loss = equipment.Element("TRUB_CHILLER_LOSS").Value,
                Evap_Rate = equipment.Element("EVAP_RATE").Value,
                Boil_Time = equipment.Element("BOIL_TIME").Value,
                Calc_Boil_Volume = equipment.Element("CALC_BOIL_VOLUME").Value,
                Lauter_Deadspace = equipment.Element("LAUTER_DEADSPACE").Value,
                Top_Up_Kettle = equipment.Element("TOP_UP_KETTLE").Value,
                Hop_Utilization = equipment.Element("HOP_UTILIZATION").Value,
                Cooling_Loss_Pct = equipment.Element("COOLING_LOSS_PCT").Value,
                Notes = equipment.Element("NOTES").Value,
                Display_Boil_Size = equipment.Element("DISPLAY_BOIL_SIZE").Value,
                Display_Batch_Size = equipment.Element("DISPLAY_BATCH_SIZE").Value,
                Display_Tun_Volume = equipment.Element("DISPLAY_TUN_VOLUME").Value,
                Display_Tun_Weight = equipment.Element("DISPLAY_TUN_WEIGHT").Value,
                Display_Top_Up_Water = equipment.Element("DISPLAY_TOP_UP_WATER").Value,
                Display_Trub_Chiller_Loss = equipment.Element("DISPLAY_TRUB_CHILLER_LOSS").Value,
                Display_Lauter_Deadspace = equipment.Element("DISPLAY_LAUTER_DEADSPACE").Value,
                Display_Top_Up_Kettle = equipment.Element("DISPLAY_TOP_UP_KETTLE").Value,
            };
            XElement mash = generalElement.Element("MASH");
            recipe.Mash = new Mash()
            {
                Name = mash.Element("NAME").Value,
                Version = mash.Element("VERSION").Value,
                Grain_Temp = mash.Element("GRAIN_TEMP").Value,
                Tun_Temp = mash.Element("TUN_TEMP").Value,
                Sparge_Temp = mash.Element("SPARGE_TEMP").Value,
                PH = mash.Element("PH").Value,
                Tun_Weight = mash.Element("TUN_WEIGHT").Value,
                Tun_Specific_Heat = mash.Element("TUN_SPECIFIC_HEAT").Value,
                Equip_Adjust = mash.Element("EQUIP_ADJUST").Value,
                Notes = mash.Element("NOTES").Value,
                Display_Grain_Temp = mash.Element("DISPLAY_GRAIN_TEMP").Value,
                Display_Tun_Temp = mash.Element("DISPLAY_TUN_TEMP").Value,
                Display_Sparge_Temp = mash.Element("DISPLAY_SPARGE_TEMP").Value,
                Display_Tun_Weight = mash.Element("DISPLAY_TUN_WEIGHT").Value,
                Mash_Steps = (from c in mash.Descendants("MASH_STEP")
                              select new Mash_Step()
                              {
                                  Name = c.Element("NAME").Value,
                                  Version = c.Element("VERSION").Value,
                                  Type = c.Element("TYPE").Value,
                                  Infuse_Amount = c.Element("INFUSE_AMOUNT").Value,
                                  Step_Time = c.Element("STEP_TIME").Value,
                                  Step_Temp = c.Element("STEP_TEMP").Value,
                                  Ramp_Time = c.Element("RAMP_TIME").Value,
                                  End_Temp = c.Element("END_TEMP").Value,
                                  Decsription = c.Element("DESCRIPTION").Value,
                                  Water_Grain_Ratio = c.Element("WATER_GRAIN_RATIO").Value,
                                  Decotion_Amt = c.Element("DECOCTION_AMT").Value,
                                  Infuse_Temp = c.Element("INFUSE_TEMP").Value,
                                  Display_Step_Temp = c.Element("DISPLAY_STEP_TEMP").Value,
                                  Display_Infuse_Amt = c.Element("DISPLAY_INFUSE_AMT").Value
                              }).ToList<Mash_Step>()
            };
            recipe.Notes = generalElement.Element("NOTES").Value;
            recipe.Taste_Notes = generalElement.Element("TASTE_NOTES").Value;
            recipe.Taste_Rating = generalElement.Element("TASTE_RATING").Value;
            recipe.OG = generalElement.Element("OG").Value;
            recipe.FG = generalElement.Element("FG").Value;
            recipe.Carbonation = generalElement.Element("CARBONATION").Value;
            recipe.Fermentation_Stages = generalElement.Element("FERMENTATION_STAGES").Value;
            recipe.Primary_Age = generalElement.Element("PRIMARY_AGE").Value;
            recipe.Primary_Temp = generalElement.Element("PRIMARY_TEMP").Value;
            recipe.Secondary_Age = generalElement.Element("SECONDARY_AGE").Value;
            recipe.Secondary_Temp = generalElement.Element("SECONDARY_TEMP").Value;
            recipe.Tertiary_Age = generalElement.Element("TERTIARY_AGE").Value;
            recipe.Age = generalElement.Element("AGE").Value;
            recipe.Age_Temp = generalElement.Element("AGE_TEMP").Value;
            recipe.Carbonation_Used = generalElement.Element("CARBONATION_USED").Value;
            recipe.Forced_Carbonation = generalElement.Element("FORCED_CARBONATION").Value;
            recipe.Priming_Sugar_Name = generalElement.Element("PRIMING_SUGAR_NAME").Value;
            recipe.Priming_Sugar_Equiv = generalElement.Element("PRIMING_SUGAR_EQUIV").Value;
            recipe.Keg_Priming_Factor = generalElement.Element("KEG_PRIMING_FACTOR").Value;
            recipe.Carbonation_Temp = generalElement.Element("CARBONATION_TEMP").Value;
            recipe.Display_Carb_Temp = generalElement.Element("DISPLAY_CARB_TEMP").Value;
            recipe.Date = generalElement.Element("DATE").Value;
            recipe.EST_OG = generalElement.Element("EST_OG").Value;
            recipe.EST_FG = generalElement.Element("EST_FG").Value;
            recipe.EST_Color = generalElement.Element("EST_COLOR").Value;
            recipe.IBU = generalElement.Element("IBU").Value;
            recipe.IBU_Method = generalElement.Element("IBU_METHOD").Value;
            recipe.EST_ABV = generalElement.Element("EST_ABV").Value;
            recipe.ABV = generalElement.Element("ABV").Value;
            recipe.Actual_Efficiency = generalElement.Element("ACTUAL_EFFICIENCY").Value;
            recipe.Calories = generalElement.Element("CALORIES").Value;
            recipe.Display_Batch_Size = generalElement.Element("DISPLAY_BATCH_SIZE").Value;
            recipe.Display_Boil_Size = generalElement.Element("DISPLAY_BOIL_SIZE").Value;
            recipe.Display_OG = generalElement.Element("DISPLAY_OG").Value;
            recipe.Display_FG = generalElement.Element("DISPLAY_FG").Value;
            recipe.Display_Primary_Temp = generalElement.Element("DISPLAY_PRIMARY_TEMP").Value;
            recipe.Display_Secondary_Temp = generalElement.Element("DISPLAY_SECONDARY_TEMP").Value;
            recipe.Display_Tertiary_Temp = generalElement.Element("DISPLAY_TERTIARY_TEMP").Value;
            recipe.Display_Age_Temp = generalElement.Element("DISPLAY_AGE_TEMP").Value;
            return recipe;
        }

        public static void WriteXML(Recipe r, string name)
        {
            XDocument doc = new XDocument(new XDeclaration("1.0", "ISO-8859-1", "YES"));
            doc.Add(new XElement("RECIPES",
                new XElement("RECIPE")));
            XElement x = doc.Element("RECIPES").Element("RECIPE");
            x.Add(new XElement("NAME", r.Name));
            x.Add(new XElement("VERSION", r.Version));
            x.Add(new XElement("TYPE", r.Type));
            x.Add(new XElement("BREWER", r.Brewer));
            x.Add(new XElement("ASST_BREWER", r.Asst_Brewer));
            x.Add(new XElement("BATCH_SIZE", r.Batch_Size));
            x.Add(new XElement("BOIL_SIZE", r.Boil_Size));
            x.Add(new XElement("BOIL_TIME", r.Boil_Time));
            x.Add(new XElement("EFFICIENCY", r.Efficiency));
            x.Add(new XElement("HOPS"));
            XElement hops = x.Element("HOPS");
            foreach (var h in r.Hops)
            {
                hops.Add(new XElement("HOP",
                    new XElement("NAME", h.Name),
                    new XElement("VERSION", h.Version),
                    new XElement("ORIGIN", h.Origin),
                    new XElement("ALPHA", h.Alpha),
                    new XElement("AMOUNT", h.Amount),
                    new XElement("USE", h.Use),
                    new XElement("TIME", h.Time),
                    new XElement("NOTES", h.Notes),
                    new XElement("TYPE", h.Type),
                    new XElement("FORM", h.Form),
                    new XElement("BETA", h.Beta),
                    new XElement("HSI", h.Hsi),
                    new XElement("DISPLAY_AMOUNT", h.Display_Amount),
                    new XElement("INVENTORY", h.Inventory),
                    new XElement("DISPLAY_TIME", h.Display_Time)
                ));
            }
            x.Add(new XElement("FERMENTABLES"));
            XElement fers = x.Element("FERMENTABLES");
            foreach (var f in r.Fermentables)
            {
                fers.Add(new XElement("FERMENTABLE",
                    new XElement("NAME", f.Name),
                    new XElement("VERSION", f.Version),
                    new XElement("TYPE", f.Type),
                    new XElement("AMOUNT", f.Amount),
                    new XElement("YIELD", f.Yield),
                    new XElement("COLOR", f.Color),
                    new XElement("ADD_AFTER_BOIL", f.Add_After_Boil),
                    new XElement("ORIGIN", f.Origin),
                    new XElement("SUPPLIER", f.Supplier),
                    new XElement("NOTES", f.Notes),
                    new XElement("COARSE_FINE_DIFF", f.Coarse_Fine_Diff),
                    new XElement("MOISTURE", f.Moisture),
                    new XElement("DIASTATIC_POWER", f.Diastatic_Power),
                    new XElement("PROTEIN", f.Protein),
                    new XElement("MAX_IN_BATCH", f.Max_In_Batch),
                    new XElement("RECOMMEND_MASH", f.Recommend_Mash),
                    new XElement("IBU_GAL_PER_LB", f.IBU_Gal_Per_LB),
                    new XElement("DISPLAY_AMOUNT", f.Display_Amount),
                    new XElement("INVENTORY", f.Inventory),
                    new XElement("POTENTIAL", f.Potential),
                    new XElement("DISPLAY_COLOR", f.Display_Color),
                    new XElement("EXTRACT_SUBSTITUTE", f.Extract_Substitude)

                ));
            }
            x.Add(new XElement("MISCS"));
            XElement miscs = x.Element("MISCS");
            foreach (var m in r.Miscs)
            {
                miscs.Add(new XElement("MISC",
                    new XElement("NAME", m.Name),
                    new XElement("VERSION", m.Version),
                    new XElement("TYPE", m.Type),
                    new XElement("USE", m.Use),
                    new XElement("AMOUNT", m.Amount),
                    new XElement("TIME", m.Time),
                    new XElement("AMOUNT_IS_WEIGHT", m.Amount_Is_Weight),
                    new XElement("USE_FOR", m.Use_For),
                    new XElement("NOTES", m.Notes),
                    new XElement("DISPLAY_AMOUNT", m.Display_Amount),
                    new XElement("INVENTORY", m.Inventory),
                    new XElement("DISPLAY_TIME", m.Display_Time),
                    new XElement("BATCH_SIZE", m.Batch_Size)
                ));
            }
            x.Add(new XElement("YEASTS"));
            XElement yeasts = x.Element("YEASTS");
            foreach (var y in r.Yeasts)
            {
                yeasts.Add(new XElement("YEAST",
                    new XElement("NAME", y.Name),
                    new XElement("VERSION", y.Version),
                    new XElement("TYPE", y.Type),
                    new XElement("FORM", y.Form),
                    new XElement("AMOUNT", y.Amount),
                    new XElement("AMOUNT_IS_WEIGHT", y.Amount_Is_Weight),
                    new XElement("LABORATORY", y.Laboratory),
                    new XElement("PRODUCT_ID", y.Product_ID),
                    new XElement("MIN_TEMPERATURE", y.Min_Temperature),
                    new XElement("MAX_TEMPERATURE", y.Max_Temperature),
                    new XElement("FLOCCULATION", y.Flocculation),
                    new XElement("ATTENUATION", y.Attenuation),
                    new XElement("NOTES", y.Notes),
                    new XElement("BEST_FOR", y.Best_For),
                    new XElement("MAX_REUSE", y.Max_Reuse),
                    new XElement("TIMES_CULTURED", y.Time_Cultured),
                    new XElement("ADD_TO_SECONDARY", y.Add_To_Secondary),
                    new XElement("DISPLAY_AMOUNT", y.Display_Amount),
                    new XElement("DISP_MIN_TEMP", y.Disp_Min_Temp),
                    new XElement("DISP_MAX_TEMP", y.Disp_Max_Temp),
                    new XElement("INVENTORY", y.Inventory),
                    new XElement("CULTURE_DATE", y.Culture_Date)
                ));
            }
            x.Add(new XElement("WATERS"));
            XElement waters = x.Element("WATERS");
            foreach (var w in r.Waters)
            {
                waters.Add(new XElement("WATER",
                    new XElement("NAME", w.Name),
                    new XElement("VERSION", w.Version),
                    new XElement("AMOUNT", w.Amount),
                    new XElement("CALCIUM", w.Calcium),
                    new XElement("BICARBONATE", w.Bicarbonate),
                    new XElement("SULFATE", w.Sulfate),
                    new XElement("CHLORIDE", w.Chloride),
                    new XElement("SODIUM", w.Sodium),
                    new XElement("MAGNESIUM", w.Magiesium),
                    new XElement("PH", w.PH),
                    new XElement("NOTES", w.Notes),
                    new XElement("DISPLAY_AMOUNT", w.Display_Amount)
                ));
            }
            x.Add(new XElement("STYLE",
                new XElement("NAME", r.Style.Name),
                new XElement("VERSION", r.Style.Version),
                new XElement("CATEGORY", r.Style.Category),
                new XElement("CATEGORY_NUMBER", r.Style.Category_Number),
                new XElement("STYLE_LETTER", r.Style.Style_Letter),
                new XElement("STYLE_GUIDE", r.Style.Style_Guide),
                new XElement("TYPE", r.Style.Type),
                new XElement("OG_MIN", r.Style.OG_Min),
                new XElement("OG_MAX", r.Style.OG_Max),
                new XElement("FG_MIN", r.Style.FG_Min),
                new XElement("FG_MAX", r.Style.FG_Max),
                new XElement("IBU_MIN", r.Style.IBU_Min),
                new XElement("IBU_MAX", r.Style.IBU_Max),
                new XElement("COLOR_MIN", r.Style.Color_Min),
                new XElement("COLOR_MAX", r.Style.Color_Max),
                new XElement("CARB_MIN", r.Style.Carb_Min),
                new XElement("CARB_MAX", r.Style.Carb_Max),
                new XElement("ABV_MIN", r.Style.ABV_Min),
                new XElement("ABV_MAX", r.Style.ABV_Max),
                new XElement("NOTES", r.Style.Notes),
                new XElement("PROFILE", r.Style.Profile),
                new XElement("INGREDIENTS", r.Style.Ingredients),
                new XElement("EXAMPLES", r.Style.Examples),
                new XElement("DISPLAY_OG_MIN", r.Style.Display_OG_Min),
                new XElement("DISPLAY_OG_MAX", r.Style.Display_OG_Max),
                new XElement("DISPLAY_FG_MIN", r.Style.Display_FG_Min),
                new XElement("DISPLAY_FG_MAX", r.Style.Display_FG_Max),
                new XElement("DISPLAY_COLOR_MIN", r.Style.Display_Color_Min),
                new XElement("DISPLAY_COLOR_MAX", r.Style.Display_Color_Max),
                new XElement("OG_RANGE", r.Style.OG_Range),
                new XElement("FG_RANGE", r.Style.FG_Range),
                new XElement("IBU_RANGE", r.Style.IBU_Range),
                new XElement("CARB_RANGE", r.Style.Carb_Range),
                new XElement("COLOR_RANGE", r.Style.Color_Range),
                new XElement("ABV_RANGE", r.Style.ABV_Range)
                ));
            x.Add(new XElement("EQUIPMENT",
                new XElement("NAME", r.Equipment.Name),
                new XElement("VERSION", r.Equipment.Version),
                new XElement("BOIL_SIZE", r.Equipment.Boil_Size),
                new XElement("BATCH_SIZE", r.Equipment.Batch_Size),
                new XElement("TUN_VOLUME", r.Equipment.Tun_Volume),
                new XElement("TUN_WEIGHT", r.Equipment.Tun_Weight),
                new XElement("TUN_SPECIFIC_HEAT", r.Equipment.Tun_Specific_Heat),
                new XElement("TOP_UP_WATER", r.Equipment.Top_Up_Water),
                new XElement("TRUB_CHILLER_LOSS", r.Equipment.Trub_Chiller_Loss),
                new XElement("EVAP_RATE", r.Equipment.Evap_Rate),
                new XElement("BOIL_TIME", r.Equipment.Boil_Time),
                new XElement("CALC_BOIL_VOLUME", r.Equipment.Calc_Boil_Volume),
                new XElement("LAUTER_DEADSPACE", r.Equipment.Lauter_Deadspace),
                new XElement("TOP_UP_KETTLE", r.Equipment.Top_Up_Kettle),
                new XElement("HOP_UTILIZATION", r.Equipment.Hop_Utilization),
                new XElement("COOLING_LOSS_PCT", r.Equipment.Cooling_Loss_Pct),
                new XElement("NOTES", r.Equipment.Notes),
                new XElement("DISPLAY_BOIL_SIZE", r.Equipment.Display_Boil_Size),
                new XElement("DISPLAY_BATCH_SIZE", r.Equipment.Display_Batch_Size),
                new XElement("DISPLAY_TUN_VOLUME", r.Equipment.Display_Tun_Volume),
                new XElement("DISPLAY_TUN_WEIGHT", r.Equipment.Display_Tun_Weight),
                new XElement("DISPLAY_TOP_UP_WATER", r.Equipment.Display_Top_Up_Water),
                new XElement("DISPLAY_TRUB_CHILLER_LOSS", r.Equipment.Display_Trub_Chiller_Loss),
                new XElement("DISPLAY_LAUTER_DEADSPACE", r.Equipment.Display_Lauter_Deadspace),
                new XElement("DISPLAY_TOP_UP_KETTLE", r.Equipment.Display_Top_Up_Kettle)
                ));
            x.Add(new XElement("MASH",
                new XElement("NAME", r.Mash.Name),
                new XElement("VERSION", r.Mash.Version),
                new XElement("GRAIN_TEMP", r.Mash.Grain_Temp),
                new XElement("TUN_TEMP", r.Mash.Tun_Temp),
                new XElement("SPARGE_TEMP", r.Mash.Sparge_Temp),
                new XElement("PH", r.Mash.PH),
                new XElement("TUN_WEIGHT", r.Mash.Tun_Weight),
                new XElement("TUN_SPECIFIC_HEAT", r.Mash.Tun_Specific_Heat),
                new XElement("EQUIP_ADJUST", r.Mash.Equip_Adjust),
                new XElement("NOTES", r.Mash.Notes),
                new XElement("DISPLAY_GRAIN_TEMP", r.Mash.Display_Grain_Temp),
                new XElement("DISPLAY_TUN_TEMP", r.Mash.Display_Tun_Temp),
                new XElement("DISPLAY_SPARGE_TEMP", r.Mash.Display_Sparge_Temp),
                new XElement("DISPLAY_TUN_WEIGHT", r.Mash.Display_Tun_Weight),
                new XElement("MASH_STEPS")
                ));
            XElement mash_steps = x.Element("MASH").Element("MASH_STEPS");
            foreach (var m_s in r.Mash.Mash_Steps)
            {
                mash_steps.Add(new XElement("MASH_STEP",
                        new XElement("NAME", m_s.Name),
                        new XElement("VERSION", m_s.Version),
                        new XElement("TYPE", m_s.Type),
                        new XElement("INFUSE_AMOUNT", m_s.Infuse_Amount),
                        new XElement("STEP_TIME", m_s.Step_Time),
                        new XElement("STEP_TEMP", m_s.Step_Temp),
                        new XElement("RAMP_TIME", m_s.Ramp_Time),
                        new XElement("END_TEMP", m_s.End_Temp),
                        new XElement("DESCRIPTION", m_s.Decsription),
                        new XElement("WATER_GRAIN_RATIO", m_s.Water_Grain_Ratio),
                        new XElement("DECOCTION_AMT", m_s.Decotion_Amt),
                        new XElement("INFUSE_TEMP", m_s.Infuse_Temp),
                        new XElement("DISPLAY_STEP_TEMP", m_s.Display_Step_Temp),
                        new XElement("DISPLAY_INFUSE_AMT", m_s.Display_Infuse_Amt)
                    ));
            }
            x.Add(new XElement("NOTES", r.Notes));
            x.Add(new XElement("TASTE_NOTES", r.Taste_Notes));
            x.Add(new XElement("TASTE_RATING", r.Taste_Rating));
            x.Add(new XElement("OG", r.OG));
            x.Add(new XElement("FG", r.FG));
            x.Add(new XElement("CARBONATION", r.Carbonation));
            x.Add(new XElement("FERMENTATION_STAGES", r.Fermentation_Stages));
            x.Add(new XElement("PRIMARY_AGE", r.Primary_Age));
            x.Add(new XElement("PRIMARY_TEMP", r.Primary_Temp));
            x.Add(new XElement("SECONDARY_AGE", r.Secondary_Age));
            x.Add(new XElement("SECONDARY_TEMP", r.Secondary_Temp));
            x.Add(new XElement("TERTIARY_AGE", r.Tertiary_Age));
            x.Add(new XElement("AGE", r.Age));
            x.Add(new XElement("AGE_TEMP", r.Age_Temp));
            x.Add(new XElement("CARBONATION_USED", r.Carbonation_Used));
            x.Add(new XElement("FORCED_CARBONATION", r.Forced_Carbonation));
            x.Add(new XElement("PRIMING_SUGAR_NAME", r.Priming_Sugar_Name));
            x.Add(new XElement("PRIMING_SUGAR_EQUIV", r.Priming_Sugar_Equiv));
            x.Add(new XElement("KEG_PRIMING_FACTOR", r.Keg_Priming_Factor));
            x.Add(new XElement("CARBONATION_TEMP", r.Carbonation_Temp));
            x.Add(new XElement("DISPLAY_CARB_TEMP", r.Display_Carb_Temp));
            x.Add(new XElement("DATE", r.Date));
            x.Add(new XElement("EST_OG", r.EST_OG));
            x.Add(new XElement("EST_FG", r.EST_FG));
            x.Add(new XElement("EST_COLOR", r.EST_Color));
            x.Add(new XElement("IBU", r.IBU));
            x.Add(new XElement("IBU_METHOD", r.IBU_Method));
            x.Add(new XElement("EST_ABV", r.EST_ABV));
            x.Add(new XElement("ABV", r.ABV));
            x.Add(new XElement("ACTUAL_EFFICIENCY", r.Actual_Efficiency));
            x.Add(new XElement("CALORIES", r.Calories));
            x.Add(new XElement("DISPLAY_BATCH_SIZE", r.Display_Batch_Size));
            x.Add(new XElement("DISPLAY_BOIL_SIZE", r.Display_Boil_Size));
            x.Add(new XElement("DISPLAY_OG", r.Display_OG));
            x.Add(new XElement("DISPLAY_FG", r.Display_FG));
            x.Add(new XElement("DISPLAY_PRIMARY_TEMP", r.Display_Primary_Temp));
            x.Add(new XElement("DISPLAY_SECONDARY_TEMP", r.Display_Secondary_Temp));
            x.Add(new XElement("DISPLAY_TERTIARY_TEMP", r.Display_Tertiary_Temp));
            x.Add(new XElement("DISPLAY_AGE_TEMP", r.Display_Age_Temp));
            doc.Save(name);
        }
        public static bool XMLValidate(string xmlUrl)
        {
            XmlSchemaSet xs = new XmlSchemaSet();
            xs.Add(null, "BeerXML1.xsd");
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas = xs;
            settings.ValidationEventHandler += ValidationError;
            // create the XmlReader object
            XmlReader reader = XmlReader.Create(xmlUrl, settings);
            // parse the file
            while (reader.Read()) ; // empty body
            reader.Close(); // close reader stream 
            if (stringBuilder.ToString() == String.Empty)
                return true;
            else
                return false;  
        }

        private static void ValidationError(object sender, ValidationEventArgs e)
        {
            stringBuilder.Append("Validation error: " + e.Message + "\n"); 
        }
    }
}
